<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../../static/css/font.css">
        <link rel="stylesheet" href="../../static/css/xadmin.css">
        <script src="../../static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../static/js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="#">首页</a>
            <a href="#">s3管理</a>
            <a>
              <cite>bucket列表</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <select name="account" lay-filter="account" id="account" required="" lay-verify="required">
                                    </select>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="add"><i class="layui-icon"></i>添加bucket</button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-body "id="bucket" lay-filter="bucket" lay-data="{id: 'bucket'}">

                        </div>

                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['laydate','form','table'], function(){
        let laydate = layui.laydate;
        let form = layui.form;
        let table = layui.table;
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });

        form.verify({
            required:function (value,item) {
                let msg="必填项不能为空";
                value= $.trim(value);
                let isEmpty=!value || value.length<1;
               // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                if(item.tagName=='SELECT' && isEmpty){
                    $("html").animate({
                        scrollTop:$(item).siblings(".layui-form-select").offset().top-74
                    },50);
                }
                if(isEmpty){
                    return msg;
                }
            }
        });

        $.ajax({
          type: 'post',
          url: '/AwsAccount/',
          dataType: 'json',
          success: function(res){
              let data = res.data;
              $('#account').append('<option value="" selected="true" disabled="disabled">---请选择账号---</option>');
              $.each(data, function (index, item) {
                $('#account').append('<option value="'+item.name+'">'+item.name+'</option>')
              });
              form.render();
          }
        });


        form.on('submit(search)', function (data) {
            table.reload('bucket',{
                url: '/Bucket/',
                where: {account: $('#account').val()},
                page: {
                    curr: 1
                },
                loading: true
            });
            return false;
        });

        form.on('submit(add)', function (data){
              let htmlData = '<div class="layui-fluid">\n' +
                  '            <div class="layui-row">\n' +
                  '                <form class="layui-form">\n' +
                  '                  <div class="layui-form-item">\n' +
                  '                      <label for="name" class="layui-form-label">\n' +
                  '                          <span class="x-red">*</span>bucket名称\n' +
                  '                      </label>\n' +
                  '                      <div class="layui-input-inline">\n' +
                  '                          <input type="text" id="name" name="name" required="" lay-verify="required"\n' +
                  '                          autocomplete="off" class="layui-input">\n' +
                  '                      </div>\n' +
                  '                  </div>\n' +
                  '              </form>\n' +
                  '            </div>\n' +
                  '        </div>';
              var index = layer.open({
                  title: "添加bucket",
                  area: ["400px", "200px"],
                  type: 1,
                  content: htmlData,//把数据显示在弹出层上
                  btn: ['确定', '我再想想'],
                  yes: function (index, layero) {//改完，点击确定
                      let account = $('#account').val();
                      let name = layero.find("#name").val();//获得修改后的值

                      var index2 = layer.load(1);
                      //同步更新缓存对应的值
                      $.post(
                          "/BucketAdd/",
                          {account: account, name: name},
                          function (res) {

                              if (res.code === 0) {
                                  layer.msg(res.message, {time: 2000});
                                  layer.close(index);
                              } else {
                                  layer.alert(res.message);
                              }
                              layer.close(index2);

                          },
                          'json'
                      );
                  }
              });
              return false;
          });

        table.render({
          elem: '#bucket'
          , height: 600
          , url: '' //数据接口
          , method: 'post'  //
          , page: true //开启分页
          , cols: [[ //表头
              {checkbox: true}
              , {field: 'name', title: 'bucket名称'}
              , {field: 'create_time', title: '创建时间'}
              , {title: '操作', align: 'center', toolbar: '#bar'}
          ]]
        });

        table.on('tool(bucket)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
              let data = obj.data; //获得当前行数据
              let layEvent = obj.event; //获得 lay-event 对应的值
              let tr = obj.tr; //获得当前行 tr 的DOM对象

              if (layEvent === 'del') { //删除
                  layer.confirm('真的删除行么', function (index) {
                      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                      layer.close(index);
                      //向服务端发送删除指令
                      $.post(
                          "/BucketDel/",
                          data,
                          function (res) {
                              layer.msg(res.message, {time: 2000});
                          },
                          'json'
                      );
                  });
              }
          });

      });


      function delAll (argument) {

        let data = tableCheck.getData();
  
        layer.confirm('确认要删除吗？'+data,function(index){
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
      }
    </script>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    </script>
</html>